import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/utils/utils.dart';

///Title:自定义AppBar <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-02-25 <br/>
///version 1.0 <br/>
class CommonAppBar extends StatelessWidget implements PreferredSizeWidget {
  ///背景颜色
  Color backgroundColor;

  ///返回按钮图片
  String backImg;

  ///返回按钮点击
  VoidCallback backClick;

  ///显示返回按钮图片
  bool showBack;

  ///标题
  String title;

  ///标题点击
  VoidCallback titleClick;

  ///右文本
  String rightText;

  ///右图标
  String rightImg;

  ///右点击
  VoidCallback rightClick;

  ///右边actions
  List<Widget> actions;

  ///私有actions
  List<Widget> _actions = [];

  CommonAppBar(
    this.title, {
    this.backgroundColor = CommonColors.primary,
    this.backImg,
    this.backClick,
    this.showBack = true,
    this.titleClick,
    this.rightText,
    this.rightImg,
    this.rightClick,
    this.actions,
  });

  @override
  Widget build(BuildContext context) {
    _actions.clear();
    backImg = R.assetsImgCommonIconBack;

    //默认的右文本和右图标只能存在一个
    if (!Utils.strIsEmpty(rightText)) {
      var rightTextWidget = InkWell(
        onTap: rightClick,
        child: Text(
          rightText,
          style: TextStyle(
            fontSize: CommonDimens.font_size_middle,
            color: CommonColors.appbar_text,
          ),
        ),
      );

      _actions.add(rightTextWidget);
    } else {
      if (!Utils.strIsEmpty(rightImg)) {
        var rightImgWidget = InkWell(
          onTap: rightClick,
          child: Image.asset(
            rightImg,
            width: 25,
            height: 25,
          ),
        );
        _actions.add(rightImgWidget);
      }
    }
    if (actions != null) {
      _actions.insertAll(0, actions);
    }

    SystemUiOverlayStyle _overlayStyle = ThemeData.estimateBrightnessForColor(backgroundColor) == Brightness.dark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color: backgroundColor,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              Semantics(
                namesRoute: true,
                header: true,
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  child: RawMaterialButton(
                    onPressed: titleClick,
                    child: Text(
                      title,
                      style: TextStyle(fontSize: 18, color: CommonColors.appbar_text),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  margin: const EdgeInsets.symmetric(horizontal: 80.0),
                ),
              ),
              showBack
                  ? Container(
                      margin: EdgeInsets.only(left: 15),
                      child: InkWell(
                        onTap: backClick ??
                            () {
                              FocusScope.of(context).unfocus();
                              Navigator.maybePop(context);
                            },
                        child: Image.asset(
                          backImg,
                          width: 25,
                          height: 25,
                        ),
                      ),
                    )
                  : Gaps.empty,
              Positioned(
                right: 15,
                child: Row(
                  children: _actions,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
